#include "ComLogUdp.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>



static int ComLogUdpInit()
{
    int iFd;
    int iOptval = 1;
    if ((iFd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
    {
        return -1;
    }

   if (setsockopt(iFd, SOL_SOCKET, SO_BROADCAST | SO_REUSEADDR, &iOptval, sizeof(int)) < 0)
    {
        return -1;
    }
    return iFd;    
}


void ComLogUdp(char *str)
{
      static int udp_fd = -1;
      int len = 0;
      struct sockaddr_in Addr = {0};
      if(udp_fd < 0)
      {
          udp_fd = ComLogUdpInit();
      }
      memset(&Addr, 0, sizeof(struct sockaddr_in));
      Addr.sin_family = AF_INET;
      Addr.sin_addr.s_addr = inet_addr("255.255.255.255");
      Addr.sin_port = htons(COMLOG_UDP_PORT);
      len = sendto(udp_fd, str, strlen(str), 0, (struct sockaddr*)&Addr, sizeof(struct sockaddr));
      printf("%s\n", str);
}